var sysMessageTable = function() {

	return {
		init : function() {
			sysMessageTable._initShortcutOfDate();
			var tableId = "sys_message";
			util.initCheckBox(tableId);
			var checkBoxDivStr = '<div class="checker" onclick="util.selectCheckBox(\'' + tableId + '\', this)"><span><input type="checkbox" class="checkboxes"></span></div>';

			//查询字典信息列表
			function queryData(sSource, aoData, fnCallback) {
				aoData.push({
					"name" : "pushDateStart",
					"value" : $("#pushTimeStart").val().trim()
				});
				aoData.push({
					"name" : "pushDateEnd",
					"value" : $("#pushTimeEnd").val()
				});
				$.ajax({
					"type" : "post",
					"url" : sSource,
					"dataType" : "json",
					"data" : {
						"aoData" : JSON.stringify(aoData)
					},
					"success" : function(resp) {
						fnCallback(resp);
					}
				});
			};

			$('#sys_message').DataTable({
				"processing" : true,
				"serverSide" : true,
				"sAjaxSource" : "main/message/getMessage.mvc",
				"fnServerData" : queryData,
				"pageLength" : 10,
				"sDom" : "<'row-fluid'r>t<'row-fluid'<'span6'i><'span6'p>>",
				"oLanguage" : util.getDToLanguage(),
				"columns" : [ {
					"data" : "pkId",
					"render" : function(data, type, full, meta) {
						 return '<div class="checker" onclick="util.selectCheckBox(\'' + tableId + '\', this)"><span><input type="checkbox" class="checkboxes"/></span></div>';
					},
					"bSortable" : false
				},
				{
					"data" : "messageContent",
					"bSortable" : false,
					"render" : function(data, type, full, meta) { 
						// debugger
						if('1' == full.messageStatus ) {
							return "<a href=\"javascript:;\" data-pkid='"+full.pkId+"' class=\"already-read\">"+data+"</span>";
						} else {
							return "<a href=\"javascript:;\" data-pkid='"+full.pkId+"' class=\"unread\">"+data+"</span>";
						}
					},
					"defaultContent": ""
				},
				{
					"data" : "pushDateStr",
					"bSortable" : false,
					"defaultContent": ""
				}
				],
				"fnDrawCallback" : function(oSetting) {
					util.clearSelected(tableId);
				}
			});

			$("#sys_message").on('click', 'a', function(e) {
				e.preventDefault();
				var pkId = $(this).attr("data-pkId");
				$(this).removeClass('unread').addClass('already-read');
				var _message = getMessageById(pkId);
				getMessageNotification();
				$("#messageContent").val(_message.messageContent);
				$("#pushDateStr").text(_message.pushDateStr)
				$("#message-detail").modal({show:true,keyboard:false});
			});

			$("#message-detail").on('click', '.close', function(e) {
				e.preventDefault();
				$("#message-detail .my-text").text("");
				$("#messageContent").val("");
			});

			//删除 -----begin
			$("#message_table_del").on('click', function(){
				var d = [];
				d = util.getCheckedData("sys_message");
				if (d.length == 0) {
					util.showModal("#_alertModal", "请选择要操作的记录！");

				} else {
					util.showModal("#_confirmModal", "您确定要删除选中的记录？", "_confirmModal_yes");
				}

				
			});

			$("#_confirmModal").on('click', '#_confirmModal_yes', function() {
				var d = [];
				d = util.getCheckedData("sys_message");
				var idArray=[];
				for (var i=0;i<d.length;i++) {
					idArray.push(d[i].pkId);
				}
				var table = $('#sys_message').DataTable();
				var currentPage =table.page();
				$.ajax({
					"type" : "post",
					"url" : "main/message/deleteMessage.mvc",
					"dataType" : "json",
					"async":false,
					"data" : {
						"pkId" : idArray.join(",")
					},
					"success" : function(resp) {
						if(resp.msg=='SUCCESS'){
							rmsBox.notify('success', {msg: '删除成功！'});
							table.page(currentPage).draw('page');
						}else {
							rmsBox.notify('error', {msg: '删除失败！',delay:3000});
						}
					}
				});
			});
			//删除 -----end
			//
			$("#message_table_readAll").on('click', function(){
				var table = $('#sys_message').DataTable();
				var currentPage =table.page();
				$.ajax({
					"type" : "post",
					"url" : "main/message/readAllMessage.mvc",
					"dataType" : "json",
					"async":false,
					"success" : function(resp) {
						if(resp.msg=='SUCCESS'){
							rmsBox.notify('success', {msg: '操作成功！'});
							table.page(currentPage).draw('page');
						}else {
							rmsBox.notify('error', {msg: '操作失败！',delay:3000});
						}
					}
				});

				
			});


			$("#message_table_read").on('click', function(){
				var d = [];
				d = util.getCheckedData("sys_message");
				if (d.length == 0) {
					util.showModal("#_alertModal", "请选择要操作的记录！");

				} else {
					var idArray=[];
					for (var i=0;i<d.length;i++) {
						idArray.push(d[i].pkId);
					}
					var table = $('#sys_message').DataTable();
					var currentPage =table.page();
					$.ajax({
						"type" : "post",
						"url" : "main/message/readMessage.mvc",
						"dataType" : "json",
						"async":false,
						"data" : {
							"pkId" : idArray.join(",")
						},
						"success" : function(resp) {
							if(resp.msg=='SUCCESS'){
								rmsBox.notify('success', {msg: '操作成功！'});
								table.page(currentPage).draw('page');
							}else {
								rmsBox.notify('error', {msg: '操作失败！',delay:3000});
							}
						}
					});
				}

				
			});

			// 查询
			$("#btn_query").click(function(e) {
				var table = $('#sys_message').DataTable();
				table.draw(true);
			});
			// 重置
			$("#btn_reset").click(function() {
				$("#customer_search_form")[0].reset();
			});



			function getMessageById(pkId) {
				var data = [];
				$.ajax({
					"type" : "post",
					"url" : "main/message/getMessageById.mvc",
					"dataType" : "json",
					"async":false,
					"data" : {
						"pkId" : pkId
					},
					"success" : function(resp) {
						data = resp.message;
					}
				});
				return data;
			}

			


			
			$(function(){
				
				$('.search-body input').bind('keyup', function(event) {
					if (event.keyCode == "13") {
						//回车执行查询
						$('.search-button #btn_query').click();
					}
				});
	
			});
		},

		_initShortcutOfDate : function() {

			$("#shortcut_time").on('change', function(e) {
				var _dateType = $("#shortcut_time").val();
				$.ajax({
					"type": "post",
					"url": "main/realtimeBoard/getShortcutOfDate.mvc",
					"async": true,
					"dataType": "json",
					"success": function(res) {
						if (_dateType == '0') {
							$("#pushTimeStart").val(res.yesterdayStart);
							$("#pushTimeEnd").val(res.yesterdayEnd);
						} else if (_dateType == '1') {
							$("#pushTimeStart").val(res.beginDayOfLastWeek);
							$("#pushTimeEnd").val(res.endDayOfLastWeek);
						} else if (_dateType == '2') {
							$("#pushTimeStart").val(res.beginDayOfLastMonth);
							$("#pushTimeEnd").val(res.endDayOfLastMonth);
						} else if (_dateType == '3') {
							$("#pushTimeStart").val(res.beginOfLastQuarter);
							$("#pushTimeEnd").val(res.endOfLastQuarter);
						} else if (_dateType == '4') {
							$("#pushTimeStart").val(res.beginOfLastYear);
							$("#pushTimeEnd").val(res.endOfLastYear);
						}  else if (_dateType == '5') {
							$("#pushTimeStart").val('');
							$("#pushTimeEnd").val('');
						}
					}
				});
			});
			
		}
	};
}();